package com.shareyi.joywindow.window;

import com.shareyi.joywindow.JoyWindowIntf;
import com.shareyi.joywindow.WindowConfig;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by david on 2016/6/9.
 */
public class JoyWindowSwing implements JoyWindowIntf,HyperlinkListener{


    private static JoyWindowSwing joyWindow;

    JFrame jFrame;

    FileChooserHelper fileChooserHelper;

    //文本编辑器内容
    JEditorPane editorPane;


    public void initWindow(WindowConfig config) {


    }

    public boolean checkSessionId(String sessionId) {
        return true;
    }

    public void closeWindow() {

    }

    public void openWindow() {
          //jFrame.pack();
        jFrame.pack();
        jFrame.setSize(900, 560);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int)(toolkit.getScreenSize().getWidth()-jFrame.getWidth())/2;
        int y = (int)(toolkit.getScreenSize().getHeight()-jFrame.getHeight()) / 2;
        jFrame.setLocation(x, y);
        jFrame.setVisible(true);
    }

    public void init() throws Exception {
        this.jFrame=new JFrame();
        this.jFrame.setTitle("joyWindow应用程序");
        this.jFrame.setResizable(true);
        jFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
        jFrame.setPreferredSize(new Dimension(900, 560));
        jFrame.setLocationRelativeTo(null);
        //如果没有这一句，在点击关闭Frame的时候程序其实还是在执行状态中的，加上这一句才算是真正的把资源释放掉了
        this.jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);


        this.editorPane = new JEditorPane();
        //放到滚动窗格中才能滚动查看所有内容
        JScrollPane scrollPane = new JScrollPane(editorPane);
        //设置是显示网页 html 文件,可选项
        editorPane.setContentType("text/html");
        //设置成只读，如果是可编辑，你会看到显示的样子也是不一样的，true显示界面
        editorPane.setEditable(false);
        editorPane.setText("<html><h1>说明</h1> 本窗口仅仅提供文件选择器功能，体验较差，请使用windows版本的！</html>");

        //要能响应网页中的链接，则必须加上超链监听器
        editorPane.addHyperlinkListener((HyperlinkListener) this);
        jFrame.setContentPane(scrollPane);
        this.fileChooserHelper=new FileChooserHelper(jFrame);
        fileChooserHelper.init();
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(lookAndFeel);

        jFrame.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {}

            public void windowClosing(WindowEvent e) {}

            public void windowClosed(WindowEvent e) {
                System.exit(0);
            }

            public void windowIconified(WindowEvent e) {}

            public void windowDeiconified(WindowEvent e) {}

            public void windowActivated(WindowEvent e) { }

            public void windowDeactivated(WindowEvent e) {}
        });
    }

    public void alertMsg(final String msg) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null, msg, "提示信息", JOptionPane.WARNING_MESSAGE);
            }
        });
    }

    public String getSavePath(boolean filter, String[] filterExtensions, String[] filterNames) {
        return fileChooserHelper.getSavePath(null, filter, filterExtensions, filterNames);
    }

    public String getSavePath(String parentPath, boolean filter, String[] filterExtensions, String[] filterNames) {
        return fileChooserHelper.getSavePath(parentPath,filter,filterExtensions,filterNames);
    }

    public String getDirectoryPath(String path) {
        return fileChooserHelper.getDirectoryPath(path);
    }

    public String getOpenPath(boolean filter, String[] filterExtensions, String[] filterNames) {
        return fileChooserHelper.getOpenPath(null, filter, filterExtensions, filterNames);

    }

    public String getOpenPath(String parentPath, boolean filter, String[] filterExtensions, String[] filterNames) {
        return fileChooserHelper.getOpenPath(parentPath, filter, filterExtensions, filterNames);

    }


    public static JoyWindowIntf getInstance(){
        if(joyWindow==null){
            joyWindow=new JoyWindowSwing();
        }
        return joyWindow;
    }




    public void setText(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                editorPane.setText(text);
            }
        });
    }


    public void hyperlinkUpdate(HyperlinkEvent e) {
        if(e.getEventType()== HyperlinkEvent.EventType.ACTIVATED){
            //用来打开系统默认浏览器浏览指定的URL
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(e.getURL().toURI());
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (URISyntaxException e1) {
                e1.printStackTrace();
            }
        }
    }
}
